/*
配置阿里云签名  文档: https://help.aliyun.com/zh/api-gateway/user-guide/use-digest-authentication-to-call-an-api#topic-1867627
客户端需要从HTTP请求中生成的签名串 格式如下：
HTTPMethod
Accept
Content-MD5
Content-Type
Date
Headers
PathAndParameters
7个字段构成整个签名串，字段之间使用\n间隔，如果Headers为空，则不需要加\n，其他字段如果为空都需要保留\n。签名大小写敏感。
*/
import crtptoJs from 'crypto-js'

const appkey = '204464704'
const appsecret = 'rvrn3kxHpvxIXNz0jfu7R4d2qqa7WGCA'

// export const verifyParameters = (request: Record<string, any>) => {
//     if (request.method == 'get' && request.params) return request.params
//     if (request.method == 'get' && !request.params) return "{}"
//     if (request.method == 'post' && request.data) return request.data
//     if (request.method == 'post' && !request.data) return "{}"
// }
export const signString = (params: { method: string; path: string; data: Record<string, any> }) => {
    let {method, path, data} = params

    let headerObj = {
        'x-ca-key': appkey,
        'x-ca-nonce': uuid(),
        'x-ca-signature-method': 'HmacSHA256'
    }

    let headerText = ''
    for (let [key, value] of Object.entries(headerObj)) {
        headerText += `${key.toLowerCase()}:${value}\n`
    }
    const contentType = 'application/json'

    if (method == 'post' && Object.keys(data).length > 0) {
        const encrypData = (crtptoJs.MD5(JSON.stringify(data))).toString(crtptoJs.enc.Base64)
        const signRes = method.toLocaleUpperCase() + '\n' + '*/*' + '\n' + encrypData + '\n' + contentType + '\n' + '\n' + headerText + '/api' + path
        const enSign = (crtptoJs.HmacSHA256(signRes, appsecret)).toString(crtptoJs.enc.Base64)
        return {
            ...headerObj,
            'Accept': '*/*',
            'x-ca-signature': enSign,
            'x-ca-signature-headers': 'x-ca-key,x-ca-nonce,x-ca-signature-method',
            'content-md5': encrypData
        }
    }
    if (method == 'post' && Object.keys(data).length == 0) {
        const signRes = method.toLocaleUpperCase() + '\n' + '*/*' + '\n' + '\n' + contentType + '\n' + '\n' + headerText + '/api' + path
        const enSign = (crtptoJs.HmacSHA256(signRes, appsecret)).toString(crtptoJs.enc.Base64)
        return {
            ...headerObj,
            'Accept': '*/*',
            'x-ca-signature': enSign,
            'x-ca-signature-headers': 'x-ca-key,x-ca-nonce,x-ca-signature-method'
        }
    }
    if (method == 'get' && Object.keys(data).length > 0) {
        let pathData = ''
        for (let [key, value] of Object.entries(data)) {
            pathData = `${path}?${key}=${value}`
        }
        const signRes = method.toLocaleUpperCase() + '\n' + '*/*' + '\n' + '\n' + contentType + '\n' + '\n' + headerText + '/api' + pathData
        const enSign = (crtptoJs.HmacSHA256(signRes, appsecret)).toString(crtptoJs.enc.Base64)
        return {
            ...headerObj,
            'Accept': '*/*',
            'x-ca-signature': enSign,
            'x-ca-signature-headers': 'x-ca-key,x-ca-nonce,x-ca-signature-method',
        }
    }
    if (method == 'get' && Object.keys(data).length == 0) {
        const signRes = method.toLocaleUpperCase() + '\n' + '*/*' + '\n' + '\n' + '\n' + '\n' + headerText + '/api' + path
        const enSign = (crtptoJs.HmacSHA256(signRes, appsecret)).toString(crtptoJs.enc.Base64)
        return {
            ...headerObj,
            'Accept': '*/*',
            'x-ca-signature': enSign,
            'x-ca-signature-headers': 'x-ca-key,x-ca-nonce,x-ca-signature-method',
        }
    }

}

function uuid() {
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }

    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

